
<?php
	// form send?
	if ($_POST['action'] == 'update') {
		$this->save_settings($_POST);
	} 

	$this->load_settings();	

?>

<script type="text/javascript">
	jQuery(document).ready(function($) {

		function newid(){
			//TODO: this kinda needs something better...
			var d = new Date;
			return d.getTime();
		}

		$( "#accs_admin_settings" ).tabs({ fxFade: true, fxSpeed: 'fast' });
		$(function() {
			$( "#commonAttributes" ).dialog({
				autoOpen: false,
				show: "fade",
				hide: "fade",
				width: "360",
				height: "500"
			});

			$( "#showCommonAttributes" ).click(function() {
				$( "#commonAttributes" ).dialog( "open" );
				return false;
			});
		});

		$('#btnAdd_membermap').click(function() {
            //create a unique index for this new element
            var newIdx = newid();

            //Clone the first element in the list. clone(true) to clone event handlers as well
            //Must protect first element from being deleted.
            var newElem = $('.clonedInput_membermap:first').clone(true).attr('id', 'input' + newIdx);

            // manipulate the name/id values of the input inside the new element
            newElem.children('[name="accs_membermap_dict[]"]').attr('value', '');
            newElem.children('[class="btnDel_membermap"]').attr('id', 'btnDel' + newIdx);

            // insert the new element at the end of the list
            $('.clonedInput_membermap:last').after(newElem);

            //enable the delete buttons if there are more than one
            var length = $('.btnDel_membermap').length;
            if (length > 1)
            	$('.btnDel_membermap').each(function(idx, elem){elem.disabled = '';}); 


        });

 
        $('.btnDel_membermap').click(function(delEvent) {
        	console.log(delEvent.srcElement.id);
        	var length = $('.btnDel_membermap').length;

        	if (length > 1)
        		$('#' + delEvent.srcElement.id).parent().remove();   

        	//disable the delete buttons 
        	if (length - 1 == 1)
        		$('.btnDel_membermap').attr('disabled','disabled');
        });

        //$('btnDel').attr('disabled','disabled');
        
	});	
</script>

<div class="wrap"> <?php screen_icon(); ?> <h2>ACCS Plugin Settings</h2></div>
<div id="accs_admin_settings">	
	<ul>
		<li><a href="#audiencemap">Map AD To Audience</a></li>
		<li><a href="#email">Email Settings</a></li>
		<!--li><a href="#reqcategories">Required Categories</a></li>
		<li><a href="#other">Other</a></li-->
	</ul>
	<div id="audiencemap">
		<form action="" method="post">
		<?php
			$idstr = 'ACCS-audiencemap-settings';
			settings_fields($idstr); 
		?>
		<p>Active Directory to Audience Map Goes Here - Desc Here</p>
			<?php
				$idx = 0;
				echo '<br />';

				//Populate default if nothing set
				if(count($this->_membermap) == 0 || !is_array($this->_membermap)){
					$line  = '<div id="input1" class="clonedInput_membermap">';
					$line .= 'AD Group: <input type="text" name="accs_membermap_dict[]" value="AD Group" />';
					$line .= 'Audience: <input type="text" name="accs_membermap_dict[]" value="Targeted Audience" />';
					$line .= '<input type="button" id="btnDel1" class="btnDel_membermap" value="remove"> </div>';
					echo $line;
				}
				//Populate form with data from _membermap
				else{
					foreach ($this->_membermap as $key => $audience){
						$idx++;

						echo '<div id="input'.$idx.'" class="clonedInput_membermap">';

						$line  = 'AD Group: <input type="text" name="accs_membermap_dict[]" value="'.$key.'" />';
						$line .= 'Audience: <input type="text" name="accs_membermap_dict[]" value="'.$audience.'" />';
						$line .= '<input type="button" id="btnDel'.$idx.'" class="btnDel_membermap" value="remove">';
						echo $line;
						echo '</div>';
					}
				}
			?>
		<div>
        	<input type="button" id="btnAdd_membermap" value="Add Entry" />
    	</div>
    	<br />
		<input type="submit" class="button-primary" name="Submit" value="Save Changes"/>		
		</form>
	</div>
	<div id="email">
		<p>Email Settings Go Here</p>
		<form action="" method="post">
		<?php settings_fields('ACCS-email-settings'); ?>

        	<ul>
        		<li>Debug:     <input type="text" name="accs_email_debug"     value="<?php echo $this->_email_debug     ?>" /></li>
        		<li>Auth:      <input type="text" name="accs_email_auth"      value="<?php echo $this->_email_auth      ?>" /></li>
        		<li>Secure:    <input type="text" name="accs_email_secure"    value="<?php echo $this->_email_secure    ?>" /></li>
        		<li>Host:      <input type="text" name="accs_email_host"      value="<?php echo $this->_email_host      ?>" /></li>
        		<li>Port:      <input type="text" name="accs_email_port"      value="<?php echo $this->_email_port      ?>" /></li>
        		<li>Username:  <input type="text" name="accs_email_username"  value="<?php echo $this->_email_username  ?>" /></li>
        		<li>Password:  <input type="text" name="accs_email_password"  value="<?php echo $this->_email_password  ?>" /></li>
        		<li>From:      <input type="text" name="accs_email_from"      value="<?php echo $this->_email_from      ?>" /></li>
        		<li>From Name: <input type="text" name="accs_email_from_name" value="<?php echo $this->_email_from_name ?>" /></li>
        		<li>Alt Body:  <input type="text" name="accs_email_alt_body"  value="<?php echo $this->_email_alt_body  ?>" /></li>
        	</ul>

			<input type="submit" class="button-primary" name="Submit" value="Save Changes"/>
		</form>
	</div>
	<!--
	<div id="reqcategories">
		<p>Required Categories</p>
		<pre>
			<?php print_r(get_option(ACCS_PREFIX . 'REQUIRED_SUBSCRIPTION_CATEGORIES'))?>
		</pre>
	</div>
	<div id="other">
		<p>Other stuff like notification emails, title prefixes, etc.</p>
		<pre>
			<?php print_r(get_option(ACCS_PREFIX . 'USER_PROFILE_TITLE_PREFIX'))?>
		</pre>
	</div>
-->
</div>



